package cipher

import (
	"gitee.com/micro-plat/sas/sas/modules/const/enum"
	"gitee.com/micro-plat/sas/sas/modules/secret"
	"github.com/micro-plat/hydra"
)

//GetHandler .
type GetHandler struct {
	sLib secret.ISecret
}

//NewGetHandler .
func NewGetHandler() (u *GetHandler) {
	return &GetHandler{
		sLib: secret.NewSecret(),
	}
}

// Handle 获取密钥
func (o *GetHandler) Handle(ctx hydra.IContext) (r interface{}) {
	ctx.Log().Debug("------获取密钥------")
	ctx.Log().Debug("1.检查参数", ctx.Request().GetString("euid"))
	if err := ctx.Request().Check("euid"); err != nil {
		return err
	}

	if err := enum.CheckType(ctx.Request().Path().Params().GetString("type")); err != nil {
		return err
	}

	ctx.Log().Debug("2.获取密钥")
	res, err := o.sLib.Get(
		ctx.Request().GetString("euid"),
		ctx.Request().Path().Params().GetString("type"))
	if err != nil {
		return err
	}

	return res
}
